package com.novellotus.iov.carvice.model;


import com.novellotus.httploadingview.LoadingView;
import com.novellotus.iov.carvice.activity.DiagnosisActivity;
import com.novellotus.iov.carvice.bean.RxDiagnosisAlarm;
import com.novellotus.iov.carvice.retrofit.BaseEntity;
import com.novellotus.iov.carvice.retrofit.BaseObserver;
import com.novellotus.iov.carvice.retrofit.RetrofitFactory;
import com.novellotus.iov.carvice.retrofit.RxSchedulers;
import com.novellotus.iov.carvice.utils.SharedPreferencesUtils;

import io.reactivex.Observable;

/**
 * Created by Rich on 2017/10/17.
 */

public class DiagnosisModel {

    DiagnosisActivity diagnosisActivity;
    DiagnosisInterface diagnosisInterface;

    public DiagnosisModel(DiagnosisActivity diagnosisActivity, DiagnosisInterface diagnosisInterface) {
        this.diagnosisActivity = diagnosisActivity;
        this.diagnosisInterface = diagnosisInterface;
    }

    /**
     * @param e
     * @param r 获取自动化诊断数据
     */
    public void getDiagnosisData(String vin, String time, String e, String r, final LoadingView loadingView) {
        Observable<BaseEntity<RxDiagnosisAlarm>> observable = RetrofitFactory.getInstance().alarmsDetail(vin, SharedPreferencesUtils.getKey(diagnosisActivity)[0], time, e, r);
        observable.compose(RxSchedulers.<BaseEntity<RxDiagnosisAlarm>>compose())
                .subscribe(new BaseObserver<RxDiagnosisAlarm>(diagnosisActivity) {

                    @Override
                    public void onSuccess(RxDiagnosisAlarm rxTaskDetail) {
                        loadingView.success();
                        diagnosisInterface.setData(rxTaskDetail);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        loadingView.empty(msg, true);
                    }

                    @Override
                    public void onError(String msg) {
                        loadingView.empty(msg, true);
                    }
                });
    }

    public interface DiagnosisInterface {
        void setData(RxDiagnosisAlarm rxDiagnosisAlarm);
    }
}
